﻿using System.Collections.Generic;
using System.Linq;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service;
using MRGSP.SurveyMaster.Core.Service.Utils;

namespace MRGSP.SurveyMaster.Service.Utils
{
    public class SurveyReader : ISurveyReader
    {
        private readonly ITreeBuilder treeBuilder;

        public SurveyReader(ITreeBuilder treeBuilder)
        {
            this.treeBuilder = treeBuilder;
        }

        public IEnumerable<Answer> Read(IEnumerable<Question> questions, IDictionary<string, string> formCollection)
        {
            return treeBuilder.Build(questions)
                .SelectMany(question => ValueReaderFactory.Get(question.Type).Read(question, formCollection));
        }
    }
}